package vo;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.UUID;



public class Program implements Serializable, Comparable<Program>{

    private Program()
    {

        this.id = UUID.randomUUID().toString().replace("-", "");
    }

    public String getProperty(String phrase)
    {
        Object o = null;
        try
        {
             o = Program.class.getDeclaredField(phrase).get(this);
             if(o == null)
             {
                 o = "null";
             }
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        }
        return o.toString();

    }
    public Program(String id, String country, String university, String school, String programName, String homepage, String location, String email, String phoneNumber, String degree, String deadlineWithAid, String deadlineWithoutAid)
    {
        this.id = id;
        this.country = country;
        this.university = university;
        this.school = school;
        this.programName = programName;
        this.homepage = homepage;
        this.location = location;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.degree = degree;
        this.deadlineWithAid = deadlineWithAid;
        this.deadlineWithoutAid = deadlineWithoutAid;
    }

    public void initId()
    {
        this.id = this.getMD5();
    }
    public Program(String id)
    {

        this.id = id;
    }

    // 唯一标识id,32位随机字母数字

    private String id;

    // 国家
    private String country;

    // 学校
    private String university;

    // 学院
    private String school;

    // 项目名称
    private String programName;

    // 项目主页
    private String homepage;

    // 地址
    private String location;

    // 项目申请咨询邮箱
    private String email;

    // 联系方式
    private String phoneNumber;

    // 学位
    private String degree;

    // 申请截止时间（奖学金）
    private String deadlineWithAid;

    // 申请截止时间（无奖学金）
    private String deadlineWithoutAid;

    public static Program getInstance()
    {
        return new Program();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        initId();
        this.id = id;
    }

    public String getUniversity() {

        return university;
    }

    public void setUniversity(String university) {
        initId();
        this.university = university;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        initId();
        this.school = school;
    }

    public String getProgramName() {
        return programName;
    }

    public void setProgramName(String programName) {
        this.programName = programName;
        initId();
    }

    public String getHomepage() {
        return homepage;
    }

    public void setHomepage(String homepage) {
        initId();
        this.homepage = homepage;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        initId();
        this.location = location;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        initId();
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        initId();
        this.phoneNumber = phoneNumber;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        initId();
        this.degree = degree;
    }

    public String getDeadlineWithAid() {
        return deadlineWithAid;
    }

    public void setDeadlineWithAid(String deadlineWithAid) {
        initId();
        this.deadlineWithAid = deadlineWithAid;
    }

    public String getDeadlineWithoutAid() {
        return deadlineWithoutAid;
    }

    public void setDeadlineWithoutAid(String deadlineWithoutAid) {
        initId();
        this.deadlineWithoutAid = deadlineWithoutAid;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        initId();
        this.country = country;
    }

    public String getMD5()
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            StringBuilder message = new StringBuilder();
            for (Field field : this.getClass().getDeclaredFields())
            {
                if(!Objects.equals(field.getName(), "id"))
                {
                    message.append(this.getProperty(field.getName()));
                }
            }
            byte[] bytes = message.toString().getBytes();
            digest.update(bytes);
            return new BigInteger(digest.digest(bytes)).abs().toString(16);
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        return "None";
    }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(this.id);
        builder.append("\t");
        builder.append(this.country);
        builder.append("\t");
        builder.append(this.university);
        builder.append("\t");
        builder.append(this.school);
        builder.append("\t");
        builder.append(this.programName);
        builder.append("\t");
        builder.append(this.homepage);
        builder.append("\t");
        builder.append(this.location);
        builder.append("\t");
        builder.append(this.email);
        builder.append("\t");
        builder.append(this.phoneNumber);
        builder.append("\t");
        builder.append(this.degree);
        builder.append("\t");
        builder.append(this.deadlineWithAid);
        builder.append("\t");
        builder.append(this.deadlineWithoutAid);
        builder.append("\n");
        return builder.toString();
    }


    public int compareTo(Program o)
    {
        return this.toString().compareTo(o.toString());
    }
}
